package com.tengju.motivation.infrastructure.repository.performance;

import com.tengju.motivation.domain.model.performance.MotivationActivityConfigId;
import com.tengju.motivation.domain.model.performance.*;
import com.tengju.motivation.domain.shared.IdObject;
import com.tengju.motivation.infrastructure.persistence.mybatis.dataobject.PerformanceObjectiveDO;
import com.tengju.motivation.infrastructure.persistence.mybatis.dataobject.PerformanceObjectiveTotalDO;
import com.tengju.motivation.infrastructure.persistence.mybatis.mapper.MotivationPerformanceObjectiveMapper;
import com.tengju.motivation.infrastructure.repository.performance.convert.PerformanceObjectiveConvert;
import com.tengju.motivation.infrastructure.repository.performance.convert.PerformanceObjectiveTotalConvert;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: 李大仙
 * @date: 2021/5/15 4:35 下午
 */
@Repository
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class PerformanceObjectiveRepositoryImpl implements PerformanceObjectiveRepository {

    private final MotivationPerformanceObjectiveMapper motivationPerformanceObjectiveMapper;

    @Override
    public List<PerformanceObjective> getByObjectIdAndType(List<PerformanceObjectiveObjectId> objectIdList,
                                                           PerformanceObjectiveTypeEnum type,
                                                           Integer startTime, Integer endTime) {
        if (CollectionUtils.isEmpty(objectIdList)) {
            return Collections.emptyList();
        }
        List<Long> objectIds = objectIdList.stream().map(IdObject::getValue).collect(Collectors.toList());
        List<PerformanceObjectiveDO> list = motivationPerformanceObjectiveMapper
                .selectByObjectIdsAndType(objectIds, type.getCode(),
                        startTime, endTime);
        return list.stream()
                .map(PerformanceObjectiveConvert::convert)
                .collect(Collectors.toList());
    }

    @Override
    public List<PerformanceObjective> getByObjectIdAndKeysAndType(PerformanceObjectiveObjectId objectId,
                                                                  List<String> keys, PerformanceObjectiveTypeEnum type,
                                                                  Integer startTime, Integer endTime) {
        List<PerformanceObjectiveDO> list = motivationPerformanceObjectiveMapper.selectByObjectIdAndKeysAndType(objectId.getValue(),
                keys, type.getCode(), startTime, endTime);
        return list.stream()
                .map(PerformanceObjectiveConvert::convert)
                .collect(Collectors.toList());
    }

    @Override
    public int add(PerformanceObjective performanceObjective) {
        PerformanceObjectiveDO performanceObjectiveDO = PerformanceObjectiveConvert.convert(performanceObjective);
        return motivationPerformanceObjectiveMapper.batchInsert(List.of(performanceObjectiveDO));
    }

    @Override
    public void batchAdd(List<PerformanceObjective> performanceObjectives) {
        List<PerformanceObjectiveDO> performanceObjectiveDOList = performanceObjectives.stream()
                .map(PerformanceObjectiveConvert::convert)
                .collect(Collectors.toList());
        motivationPerformanceObjectiveMapper.batchInsert(performanceObjectiveDOList);
    }

    @Override
    public int batchRemove(PerformanceObjectiveTypeEnum type, Integer startTime, Integer endTime) {
        return motivationPerformanceObjectiveMapper.deleteByTime(type.getCode(), startTime, endTime);
    }

    @Override
    public void batchRemoveByIdCodeAndKeysAndType(PerformanceObjectiveObjectId objectId, List<String> keys,
                                                  PerformanceObjectiveTypeEnum type,
                                                  Integer startTime, Integer endTime) {
        motivationPerformanceObjectiveMapper.deleteForChunMiaoByObjectIdAndKeys(objectId.getValue(), keys, type.getCode(), startTime, endTime);
    }

    @Override
    public int remove(List<PerformanceObjectiveId> performanceObjectiveIdList) {
        List<Long> ids = performanceObjectiveIdList.stream()
                .map(IdObject::getValue)
                .collect(Collectors.toList());
        return motivationPerformanceObjectiveMapper.deleteByIds(ids);
    }

    @Override
    public int removeByRelationId(Long relationId) {
        return motivationPerformanceObjectiveMapper.deleteByRelationId(relationId);
    }

    @Override
    public int getCommonListTotal(Integer startTime,
                                  Integer endTime,
                                  Long chiefId,
                                  Integer performanceObjectiveType,
                                  Long performanceObjectiveObjectId) {
        return motivationPerformanceObjectiveMapper.getCommonListTotal(startTime, endTime, chiefId, performanceObjectiveType, performanceObjectiveObjectId);
    }

    @Override
    public int getListTotal(Integer startTime, Integer endTime, Long chiefId, Integer performanceObjectiveType, Long performanceObjectiveObjectId) {
        return motivationPerformanceObjectiveMapper.getListTotal(startTime, endTime, chiefId, performanceObjectiveType, performanceObjectiveObjectId);
    }

    @Override
    public List<PerformanceObjectiveObjectId> getObjectIdList(Integer startTime,
                                                              Integer endTime,
                                                              Long chiefId,
                                                              Integer performanceObjectiveType,
                                                              Long performanceObjectiveObjectId,
                                                              Integer offset, Integer limit) {
        List<Long> list = motivationPerformanceObjectiveMapper.getObjectIdList(startTime, endTime, chiefId, performanceObjectiveType, performanceObjectiveObjectId, offset, limit);
        return list.stream()
                .map(PerformanceObjectiveObjectId::new)
                .collect(Collectors.toList());
    }

    @Override
    public int getActivityListTotal(MotivationActivityConfigId activityId, Long chiefId, PerformanceObjectiveTypeEnum typeEnum, Long performanceObjectiveObjectId) {
        return motivationPerformanceObjectiveMapper.getActivityListTotal(activityId.getValue(), chiefId, typeEnum.getCode(), performanceObjectiveObjectId);
    }

    @Override
    public List<PerformanceObjectiveObjectId> getRelationObjectIdList(Long relationId, Long chiefId, PerformanceObjectiveTypeEnum typeEnum, Long performanceObjectiveObjectId, Integer offset, Integer limit) {
        List<Long> list = motivationPerformanceObjectiveMapper.getRelationObjectIdList(relationId, chiefId, typeEnum.getCode(), performanceObjectiveObjectId, offset, limit);
        return list.stream()
                .map(PerformanceObjectiveObjectId::new)
                .collect(Collectors.toList());
    }

    @Override
    public List<PerformanceObjectiveTotal> getPerformanceObjectiveTotalByRelationId(Long relationId) {
        List<PerformanceObjectiveTotalDO> list = motivationPerformanceObjectiveMapper.getPerformanceObjectiveTotalByRelationId(relationId);
        return list.stream()
                .map(PerformanceObjectiveTotalConvert::convert)
                .collect(Collectors.toList());
    }

    @Override
    public int updateByRelationId(Long relationId,  Integer startTime, Integer endTime) {
        return motivationPerformanceObjectiveMapper.updateByRelationId(relationId, startTime, endTime);
    }
}
